<template>
  <div class="profile">
      <!-- 未登录的头部 -->
      <div class="unlogin headder" v-if="!user">
        <img src="@/assets/images/mobile.png" alt="">
        <span @click="tologin">登录 / 注册</span>
      </div>
      <!-- 登录的头部 -->
      <div class="login headder" v-else>
        <!-- 用户基本信息 -->
        <div class="user-info">
          <div class="left">
            <img src="@/assets/images/yme.jpg" alt="">
            <span>{{userInfo.name}}</span>
          </div>
          <van-button to="/user" type="default">编辑资料</van-button>
        </div>
        <!-- 用户数据 -->
        <div class="user-data">
          <div class="data-item">
              <span>{{userInfo.art_count}}</span>
              <span>头条</span>
          </div>
          <div class="data-item">
              <span>{{userInfo.fans_count}}</span>
              <span>粉丝</span>
          </div>
          <div class="data-item">
              <span>{{userInfo.follow_count}}</span>
              <span>关注</span>
          </div>
          <div class="data-item">
              <span>{{userInfo.like_count}}</span>
              <span>获赞</span>
          </div>
        </div>
      </div>
      <!-- 导航 -->
      <van-grid :column-num="2" class="myGrid">
        <van-grid-item  text="收藏" >
            <template #icon>
              <i class="toutiao toutiao-shoucang"></i>
            </template>
        </van-grid-item>
        <van-grid-item  text="历史" >
            <template #icon>
              <i class="toutiao toutiao-lishi"></i>
            </template>
        </van-grid-item>
      </van-grid>

      <!-- 小导航 -->
      <div class="out-box">
        <van-cell title="消息通知" is-link />
        <van-cell title="小黑同学" is-link />
      </div>
      <van-cell @click="outlogin" title="退出" class="out-login" />
  </div>
</template>

<script>
import { getUserSelfInfo } from '@/api/getverify'
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
export default {
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },

  methods: {
    outlogin () {
      Dialog.confirm({
        title: '黑马头条',
        message: '是否确定退出该账号'
      })
        .then(() => {
          this.$store.commit('removeuser')
        })
        .catch(() => {})
    },
    tologin () {
      this.$router.push('/login')
    },
    async loadUserInfo () {
      try {
        const res = await getUserSelfInfo()
        this.userInfo = res
      } catch (error) {
        Toast('获取信息失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.profile {
  .headder {
    width: 750px;
    height: 401px;
    background-image: url(~@/assets/images/banner.png);
    background-size: 100% 100%;
  }
  .unlogin {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 10px;
    }
  }
  .login {
    overflow: hidden;
    .user-info{
      width: 100%;
      height: 132px;
      margin-top: 116px;
      padding: 0 32px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        img {
          width: 132px;
          height: 132px;
          border-radius: 50%;
          border: 1px solid #fff;
        }
        span{
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .van-button {
        width: 115px;
        height: 32px;
        background: #FFFFFF;
        border-radius: 16px;
        font-size: 20px;
        color: #666;
        padding: 0;
      }
    }
    .user-data {
      width: 100%;
      height: 156px;
      display: flex;
      .data-item{
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #fff;
        span:nth-child(1){
          font-size: 26px;
          margin-bottom: 8px;
        }
        span:nth-child(2){
          font-size: 22px;
        }
      }
    }
  }
  // 导航
  .myGrid {
    .toutiao {
      display: inline-block;
      margin-bottom: 10px;
      font-size: 45px;
    }

    .toutiao-shoucang {
      color: #EB5253;
    }

    .toutiao-lishi {
      color: #FF9D1D;
    }

  }
  // 小导航
  .out-box{
    margin: 10px 0;
  }
  .out-login{
    text-align: center;
    color: red;
  }
}
</style>
